<?php
/**
 *  extention.func.php 用户自定义函数库
 *
 * @copyright			(C) 2005-2010 PHPCMS
 * @license				http://www.phpcms.cn/license/
 * @lastmodify			2010-10-27
 */
 
/**
 * 获取指定分类下的产品数量
 */
function getProductNum($catids, $modelid, $bid="")
{
	$db = pc_base::load_model('content_model');
	$db->set_model($modelid);
	if (is_array($catids))
	{
		$catids = implode(',', $catids);
	}
	if (empty($bid)) {
		return $db->count('`catid` IN ('.$catids.')');
	} else {
		$brand_db = pc_base::load_model('brand_model');
		$brands = $brand_db->select("`id`=".$bid.' OR `parentid`='.$bid, 'id');
		$bids = '';
		foreach ($brands AS $v) {
			$bids .= $v['id'].',';
		}
		return $db->count('`catid` IN ('.$catids.') AND `bids` IN ('.rtrim($bids ,',').')');
	}
}

function getHotKeywords() {
	$keywords_model = pc_base::load_model('tags_model');
	$keywords = $keywords_model->select('', 'tag', '0,50', 'usetimes DESC');
	foreach($keywords AS &$k) {
		$k['tag_encode'] = urlencode($k['tag']);
	}
	return $keywords;
}

//获取最新的求助信息
function getTopMyhelp()
{
	$db = pc_base::load_model('myhelp_model');
	return  $db->select('', 'id,title,productid,hits,inputtime', '50', 'inputtime DESC');
}

/**
 * 获取最新10条清单列表 
 */
function getTopOrder($num=10) {
	$userid = param::get_cookie('_userid');
	if ($userid) {
		$order_model = pc_base::load_model('order_model');
		return $order_model->select('userid='.$userid, '*', '0,'.$num, 'selected DESC, updatetime DESC');
	} else {
		return '';
	}
}

/**
 * 获取会员列表
 */
function getAllMemeber($groupid, $userid="") {
	$member_model = pc_base::load_model('member_model');
	$where = '`groupid`='.$groupid;
	$where .= ' AND `vip`=1 AND `overduedate`>'.SYS_TIME;
	return $member_model->select($where , 'userid,nickname,username', 10000, 'username ASC');
}
/**
 * 获取单个会员信息
 */
function getOneMember($userid) {
	$member_model = pc_base::load_model('members_model');
	$member = $member_model->get_one("`userid`=".$userid);
	$member_model->set_model($member['modelid']);
	$member_ext = $member_model->get_one("`userid`=".$userid);
	return array_merge($member_ext, $member);
}

/**
* 获取父栏目
*/
function getCat($catid) {
	if (empty($siteid)) $siteid = get_siteid();
	$category = getcache('category_content_'.$siteid,'commons');
	return $category[$catid];
}

/**
* 获取等待询价的产品数量
*/
function getTotalPriceNum()
{
	$userid = param::get_cookie('_userid');
	if ($userid) {
		$order_detail_model = pc_base::load_model('order_detail_model');
		$where = 'supplierid='.$userid.' AND `enquiry`=1';
		return $order_detail_model->count($where);
	} else {
		return '';
	}
	
}

/**
 * 根据catid获取品牌列表
 */
function getparentBrands($catid) 
{
	$db = pc_base::load_model('brand_model');
	return $db->select(array('catid'=>$catid, 'parentid'=>0), 'id,name,parentid');
}

/**
 * 格式化品牌列表Url
 */
function parseBrandUrl($catid, $bid) {
	$url = SITE_PROTOCOL.SITE_URL.$_SERVER['REQUEST_URI'];
	$pathinfo = pathinfo($url);
	$new_url = $pathinfo['dirname'] . '/list-'.$catid.'-1-'.$bid.'.html';
	return $new_url;
}
?>